package com.googlecode.scenemaker3d.view
{
	import org.papervision3d.core.geom.Lines3D;
	import org.papervision3d.core.geom.renderables.Line3D;
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.materials.special.LineMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import mx.states.AddChild;
	import om.googlecode.scenemaker3d.services.Control;
	
	public class GridFragmented  extends DisplayObject3D
	
	{	
		private var lm:LineMaterial;
		private var lines:Lines3D;
		
		public function GridFragmented(width:Number, divs:uint)
		{
			lm = new LineMaterial(0x909090, .2);
			//lines = new Lines3D(lm);
			
			draw(width, divs);
		}
		private function draw(width:Number, divs:Number):void{
			var dist:Number = width / divs;
			for(var j:int=0; j<divs; j++){
				for(var i:int=0; i<divs; i++){
					var segment:Lines3D = makeSegment(width, divs);
					segment.x = dist * i - (width /2);
					segment.z = dist * j - (width /2);
					Control.instance.objectController.addObject(segment);
				}
			}
			
		}
		private function makeSegment(width:Number, divs:Number):Lines3D{
			//XZ plane
			var dist:Number = width / divs;
			//var offSet:Number = width / 2;
			var lines:Lines3D = new Lines3D(lm);
			//Draw an L shape
			var vt1:Vertex3D = new Vertex3D(0, 0, 0);
			var vt2:Vertex3D = new Vertex3D(0, 0, -dist);
			
			var l:Line3D = new Line3D(lines, lm, 2, vt1, vt2);
			
			lines.addLine(l);
			
			vt1 = new Vertex3D(0, 0, 0);
			vt2 = new Vertex3D(-dist, 0, 0);
			
			l = new Line3D(lines, lm, 2, vt1, vt2);
			
			lines.addLine(l);
			
			return lines;
		}

	}
}